package com.authority.framework.config.interceptor;

import com.authority.framework.web.domain.SysUserEntity;
import com.authority.framework.base.util.ShiroUtils;
import com.authority.framework.base.util.StringUtils;
import com.example.demo.RegisterKey;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/** 
* @Description: 登录拦截器
* @Param:  
* @return:  
* @Author: Mr.Yan 
* @Date: 2018/12/11 
*/

public class SecurityInterceptor extends HandlerInterceptorAdapter {
    // 登录地址
    @Value("${shiro.user.loginUrl}")
    private String loginUrl;

    @Value("${shiro.user.unRegisterUrl}")
    private String unRegisterUrl;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {

        RegisterKey registerKey = new RegisterKey();
        boolean register = registerKey.isRegister();
        if (!register){
            // 跳转登录
            String url = "/unRegister";
            response.sendRedirect(url);
            return false;
        }

        SysUserEntity subjectUser = ShiroUtils.getSubjectUser();
        if(StringUtils.isNotNull(subjectUser)){
            return true;
        }
        response.sendRedirect(loginUrl);
        return false;
    }
}
